package com.ruoyi.construction.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnowflakeIdUtils;
import com.ruoyi.common.utils.excel.ExcelUtils;
import com.ruoyi.common.vo.ConstructionSiteVo;
import com.ruoyi.common.vo.DisposalSiteVo;
import com.ruoyi.common.vo.TaggVo;
import com.ruoyi.modules.domain.AuditFlowLog;
import com.ruoyi.modules.domain.SoilSiteInfo;
import com.ruoyi.modules.mapper.AuditFlowLogMapper;
import com.ruoyi.modules.mapper.SoilSiteInfoMapper;
import com.ruoyi.region.domain.TSysArea;
import com.ruoyi.region.service.ITSysAreaService;
import com.ruoyi.system.util.BaiDuMapUtil;
import com.ruoyi.tagging.DisposalSiteAndProjectService;
import com.ruoyi.violation.vo.LawAuditVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.construction.mapper.ConstructionSiteMapper;
import com.ruoyi.construction.domain.ConstructionSite;
import com.ruoyi.construction.service.IConstructionSiteService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目工地信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-14
 */
@Service
public class ConstructionSiteServiceImpl implements IConstructionSiteService 
{
    private static final Logger log = LoggerFactory.getLogger(ConstructionSiteServiceImpl.class);
    @Autowired
    private ConstructionSiteMapper constructionSiteMapper;

    @Autowired
    private SoilSiteInfoMapper   soilSiteInfoMapper;

    @Autowired
    private AuditFlowLogMapper auditFlowLogMapper;


    @Autowired
    private DisposalSiteAndProjectService disposalSiteAndProjectService;


    @Autowired
    private ITSysAreaService tSysAreaService;


    /**
     * 查询项目工地信息
     * 
     * @param id 项目工地信息主键
     * @return 项目工地信息
     */
    @Override
    public ConstructionSite selectConstructionSiteById(String id)
    {
        return constructionSiteMapper.selectConstructionSiteById(id);
    }

    /**
     * 查询项目工地信息列表
     * 
     * @param constructionSite 项目工地信息
     * @return 项目工地信息
     */
    @Override
    public List<ConstructionSite> selectConstructionSiteList(ConstructionSite constructionSite)
    {
        return constructionSiteMapper.selectConstructionSiteList(constructionSite);
    }

    /**
     * 新增项目工地信息
     * 
     * @param constructionSite 项目工地信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertConstructionSite(ConstructionSite constructionSite)
    {
        SnowflakeIdUtils idWorker1 = new SnowflakeIdUtils(1, 3);
        constructionSite.setId(idWorker1.nextId()+"");
        constructionSite.setCreateTime(DateUtils.getNowDate());

        JSONObject object = BaiDuMapUtil.geocoding(constructionSite.getAddress());
        constructionSite.setLatitude(object.getString("lat"));
        constructionSite.setLongitude(object.getString("lng"));

        // todo
        String s = disposalSiteAndProjectService.inserConstructionSiteBeiDouTagging(constructionSite);
        constructionSite.setExts(s);
//       {"rspCode":"1","poiId":"916"}
        JSONObject jsonObject = JSONObject.parseObject(s);
        constructionSite.setFourConstructionSiteId(jsonObject.getString("poiId"));
        System.out.println("插入标注数据成功======"+s);

//        try {
//            // todo
//            String s = disposalSiteAndProjectService.inserConstructionSiteBeiDouTagging(constructionSite);
////            constructionSite.setFourConstructionSiteId();
//            constructionSite.setExts(s);
////            {"rspCode":"1","poiId":"916"}
//            JSONObject jsonObject = JSONObject.parseObject(s);
//            // todo
//            constructionSite.setFourConstructionSiteId(jsonObject.getString("poiId"));
//            log.info("插入标注数据成功,响应数据：{}",s);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.info("插入标注数据失败,异常原因：{}",e.getMessage());
//        }
        return constructionSiteMapper.insertConstructionSite(constructionSite);
    }

    /**
     * 修改项目工地信息
     * 
     * @param constructionSite 项目工地信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateConstructionSite(ConstructionSite constructionSite)
    {
        constructionSite.setUpdateTime(DateUtils.getNowDate());
        // todo 调用修改北斗修改接口
        String s = disposalSiteAndProjectService.updateConstructionSiteBeiDouTagging(constructionSite);
        System.out.println("标注数据修改成功，响应数据："+s);
        log.info("标注数据修改成功：{}",s);
//        try {
//            // todo 调用修改北斗修改接口
//            String s = disposalSiteAndProjectService.updateConstructionSiteBeiDouTagging(constructionSite);
//            System.out.println("标注数据修改成功，响应数据："+s);
//            log.info("标注数据修改成功：{}",s);
//        } catch (Exception e) {
//            log.error("标注数据修改失败：{}",e.getMessage());
//            throw new RuntimeException(e);
//        }
        return constructionSiteMapper.updateConstructionSite(constructionSite);
    }

    /**
     * 批量删除项目工地信息
     * 
     * @param ids 需要删除的项目工地信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteConstructionSiteByIds(String[] ids)
    {
        for (String id : ids) {
            // todo 调用北斗 删除接口
            ConstructionSite site = constructionSiteMapper.selectConstructionSiteById(id);
            String s = disposalSiteAndProjectService.delConstructionSiteBeiDouTagging(site.getFourConstructionSiteId());
            System.out.println("标注数据删除成功，响应数据："+s);
            log.info("标注数据删除成功，响应数据：{}",s);
        }


//        for (String id : ids) {
//            try {
//                // todo 调用北斗 删除接口
//                ConstructionSite site = constructionSiteMapper.selectConstructionSiteById(id);
//                String s = disposalSiteAndProjectService.delConstructionSiteBeiDouTagging(site.getFourConstructionSiteId());
//                System.out.println("标注数据删除成功，响应数据："+s);
//                log.info("标注数据删除成功，响应数据：{}",s);
//            } catch (Exception e) {
//                log.error("标注数据删除成功：{}",e.getMessage());
//                throw new RuntimeException(e);
//            }
//        }
        return constructionSiteMapper.deleteConstructionSiteByIds(ids);
    }

    /**
     * 删除项目工地信息信息
     * 
     * @param id 项目工地信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteConstructionSiteById(String id)
    {
        ConstructionSite site = constructionSiteMapper.selectConstructionSiteById(id);
        String s = disposalSiteAndProjectService.delConstructionSiteBeiDouTagging(site.getFourConstructionSiteId());
        System.out.println("标注数据删除成功，响应数据："+s);
        return constructionSiteMapper.deleteConstructionSiteById(id);
    }

    /**
     * 审核
     * @param lawAuditVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int audit(LawAuditVo lawAuditVo) {
        ConstructionSite constructionSite = constructionSiteMapper.selectConstructionSiteById(lawAuditVo.getId() + "");
        constructionSite.setAuditFlowStatus(5l);
        constructionSite.setUpdateTime(new Date());
        int i = constructionSiteMapper.updateConstructionSite(constructionSite);
        if(i>0){
            AuditFlowLog auditFlowLog = new AuditFlowLog();
            auditFlowLog.setAuditUserId(lawAuditVo.getAuditUserId());
            auditFlowLog.setAuditStatus(lawAuditVo.getAuditStatus());
            auditFlowLog.setAuditReason(lawAuditVo.getReason());
            auditFlowLog.setAuditType(0l);
            auditFlowLog.setCreateTime(new Date());
            auditFlowLog.setStatus(0l);
            auditFlowLog.setViolationReportId(Long.parseLong(constructionSite.getId()));
            i = auditFlowLogMapper.insertAuditFlowLog(auditFlowLog);
        }
        return i;
    }


    /**
     * 项目导入
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void projectDate(MultipartFile file) throws Exception {
        // 解析excel
        List<ConstructionSiteVo> dtoList = ExcelUtils.readMultipartFile(file, ConstructionSiteVo.class);
        for (ConstructionSiteVo constructionSiteVo : dtoList) {
            ConstructionSite constructionSite = new ConstructionSite();
            SnowflakeIdUtils idWorker1 = new SnowflakeIdUtils(1, 3);
            constructionSite.setId(idWorker1.nextId()+"");
            constructionSite.setName(constructionSiteVo.getName());
            constructionSite.setAddress(constructionSiteVo.getAddress());
            constructionSite.setLatitude(constructionSiteVo.getLatitude());
            constructionSite.setLongitude(constructionSiteVo.getLongitude());
            constructionSite.setUserId("1");// 先默认超级管理员
            constructionSiteMapper.insertConstructionSite(constructionSite);
        }


    }


    /**
     * 项目更新
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void projectUpdate(MultipartFile file) throws Exception {
        List<TaggVo> dtoList = ExcelUtils.readMultipartFile(file, TaggVo.class);
        for (TaggVo taggVo : dtoList) {


            ConstructionSite constructionSite = new ConstructionSite();
            constructionSite.setName(taggVo.getName());
            List<ConstructionSite> siteList = constructionSiteMapper.selectConstructionSiteList(constructionSite);

            if(siteList.size()>0){

                ConstructionSite site = new ConstructionSite();
                site.setProvinceCode("130000");
                site.setProvinceName("河北省");
                site.setCityCode("130100");
                site.setCityName("石家庄市");
                // 处理区域
                String[] strings = taggVo.getArea().split(" ");
                String string = strings[3];
                System.out.println("========="+string);
                TSysArea tSysArea = new TSysArea();
                tSysArea.setCityCode("130100");
                tSysArea.setAreaName(string);
                List<TSysArea> tSysAreas = tSysAreaService.selectTSysAreaList(tSysArea);
                if(tSysAreas.size()>0){
                    site.setAreaCode(tSysAreas.get(0).getAreaCode());
                    site.setAreaName(tSysAreas.get(0).getAreaName());
                }
                site.setId(siteList.get(0).getId());


                constructionSiteMapper.updateConstructionSite(site);
            }
        }

    }


    /**
     * 处置场所
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disposalsSite(MultipartFile file) throws Exception {
        List<DisposalSiteVo> dtoList = ExcelUtils.readMultipartFile(file, DisposalSiteVo.class);

        for (DisposalSiteVo disposalSiteVo : dtoList) {

            SoilSiteInfo siteInfo = new SoilSiteInfo();
            SnowflakeIdUtils idWorker1 = new SnowflakeIdUtils(1, 3);
            siteInfo.setId(idWorker1.nextId()+"");
            siteInfo.setSoilName(disposalSiteVo.getName());
            siteInfo.setAddress(disposalSiteVo.getAddress());
            siteInfo.setLatitude(disposalSiteVo.getLatitude());
            siteInfo.setLongitude(disposalSiteVo.getLongitude());
            siteInfo.setUserId("1");
            soilSiteInfoMapper.insertSoilSiteInfo(siteInfo);
        }



//

    }

    /**
     * 处置场所修改
     */
    @Override
    public void disposalsSiteUpdate(MultipartFile file) throws Exception {
        List<TaggVo> dtoList = ExcelUtils.readMultipartFile(file, TaggVo.class);
        for (TaggVo taggVo : dtoList) {



            SoilSiteInfo siteInfo = new SoilSiteInfo();
            siteInfo.setSoilName(taggVo.getName());
            List<SoilSiteInfo> siteList = soilSiteInfoMapper.selectSoilSiteInfoList(siteInfo);
            if(siteList.size()>0){
                SoilSiteInfo site = new SoilSiteInfo();
                site.setProvinceCode("130000");
                site.setProvinceName("河北省");
                site.setCityCode("130100");
                site.setCityName("石家庄市");
                // 处理区域
                String[] strings = taggVo.getArea().split(" ");
                String string = strings[3];
                System.out.println("========="+string);
                TSysArea tSysArea = new TSysArea();
                tSysArea.setCityCode("130100");
                tSysArea.setAreaName(string);
                List<TSysArea> tSysAreas = tSysAreaService.selectTSysAreaList(tSysArea);
                if(tSysAreas.size()>0){
                    site.setAreaCode(tSysAreas.get(0).getAreaCode());
                    site.setAreaName(tSysAreas.get(0).getAreaName());
                }
                site.setId(siteList.get(0).getId());

                soilSiteInfoMapper.updateSoilSiteInfo(site);

            }
        }
    }

    /**
     *  同步项目和场所id
     *  住建局
     */
    @Override
    public void changeId() {
        disposalSiteAndProjectService.changeId();
    }
}
